/**
 * Project: rialto.container.microcontainer
 *
 * File Created at 2010-4-16
 * $Id$
 *
 * Copyright 2008 Alibaba.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package org.complex.jbossmc;

import java.net.URL;

import org.jboss.kernel.plugins.bootstrap.basic.BasicBootstrap;
import org.jboss.kernel.plugins.deployment.xml.BasicXMLDeployer;

/**
 * Add a BasicXMLDeployer to the BasicBootstrap so that we can parse XML
 * descriptors for the beans (*-beans.xml).
 *
 * @author <a href="mailto:mark.newton@jboss.org">Mark Newton</a>
 */
public class EmbeddedBootstrap extends BasicBootstrap {


    protected BasicXMLDeployer  deployer;

    public EmbeddedBootstrap() throws Exception {
        super();
    }

    public void bootstrap() throws Throwable {
        super.bootstrap();
        deployer = new BasicXMLDeployer(getKernel());
        Runtime.getRuntime().addShutdownHook(new Shutdown());
    }

    public void deploy(URL url) throws Throwable {
        // Workaround the fact that the BasicXMLDeployer does not handle redeployment correctly
        if (deployer.getDeploymentNames().contains(url.toString())) {
            System.out.println("Service is already deployed.");
            return;
        }

        deployer.deploy(url);
    }

    public void undeploy(URL url) {
        if (!deployer.getDeploymentNames().contains(url.toString())) {
            System.out.println("Service is already undeployed.");
            return;
        }

        deployer.undeploy(url);
    }

    protected class Shutdown extends Thread {
        public void run() {
          
            deployer.shutdown();
        }
    }
}
